package com.ruoyi.system.controller;

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.MedicineReminder;
import com.ruoyi.system.service.IMedicineReminderService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 吃药提醒Controller
 * 
 * @author shan
 * @date 2025-04-13
 */
@RestController
@RequestMapping("/system/reminder")
public class MedicineReminderController extends BaseController
{
    @Autowired
    private IMedicineReminderService medicineReminderService;

    /**
     * 查询吃药提醒列表
     */
    @PreAuthorize("@ss.hasPermi('system:reminder:list')")
    @GetMapping("/list")
    public TableDataInfo list(MedicineReminder medicineReminder)
    {
        startPage();
        medicineReminder.setCreateBy(getUsername());
        List<MedicineReminder> list = medicineReminderService.selectMedicineReminderList(medicineReminder);
        return getDataTable(list);
    }

    /**
     * 查询吃药提醒列表
     */
    @PreAuthorize("@ss.hasPermi('system:reminder:list')")
    @GetMapping("/list2")
    public TableDataInfo list2(MedicineReminder medicineReminder)
    {
        medicineReminder.setCreateBy(getUsername());
        medicineReminder.setReminderCycle("单次");
        List<MedicineReminder> list = medicineReminderService.selectMedicineReminderList(medicineReminder);
        medicineReminder.setReminderCycle("每日");
        List<MedicineReminder> list2 = medicineReminderService.selectMedicineReminderList(medicineReminder);
        list.addAll(list2);
        // 获取当前日期
        LocalDate today = LocalDate.now();
        // 判断是否是当月 1 号
        boolean isFirstDayOfMonth = today.getDayOfMonth() == 1;
        // 判断是否是星期一
        boolean isMonday = today.getDayOfWeek() == DayOfWeek.MONDAY;
        if (isFirstDayOfMonth){
            medicineReminder.setReminderCycle("每月");
            List<MedicineReminder> list3 = medicineReminderService.selectMedicineReminderList(medicineReminder);
            list.addAll(list3);
        }
        if (isMonday){
            medicineReminder.setReminderCycle("每周");
            List<MedicineReminder> list3 = medicineReminderService.selectMedicineReminderList(medicineReminder);
            list.addAll(list3);
        }
        list.sort((o1, o2) -> {
            return o1.getReminderTime().compareTo(o2.getReminderTime());
        });
        return getDataTable(list);
    }


    /**
     * 导出吃药提醒列表
     */
    @PreAuthorize("@ss.hasPermi('system:reminder:export')")
    @Log(title = "吃药提醒", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MedicineReminder medicineReminder)
    {
        medicineReminder.setCreateBy(getUsername());
        List<MedicineReminder> list = medicineReminderService.selectMedicineReminderList(medicineReminder);
        ExcelUtil<MedicineReminder> util = new ExcelUtil<MedicineReminder>(MedicineReminder.class);
        util.exportExcel(response, list, "吃药提醒数据");
    }

    /**
     * 获取吃药提醒详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:reminder:query')")
    @GetMapping(value = "/{reminderId}")
    public AjaxResult getInfo(@PathVariable("reminderId") Long reminderId)
    {
        return success(medicineReminderService.selectMedicineReminderByReminderId(reminderId));
    }

    /**
     * 新增吃药提醒
     */
    @PreAuthorize("@ss.hasPermi('system:reminder:add')")
    @Log(title = "吃药提醒", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MedicineReminder medicineReminder)
    {
        medicineReminder.setCreateBy(getUsername());
        medicineReminder.setUpdateBy(getUsername());
        return toAjax(medicineReminderService.insertMedicineReminder(medicineReminder));
    }

    /**
     * 修改吃药提醒
     */
    @PreAuthorize("@ss.hasPermi('system:reminder:edit')")
    @Log(title = "吃药提醒", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MedicineReminder medicineReminder)
    {
        medicineReminder.setUpdateBy(getUserId().toString());
        return toAjax(medicineReminderService.updateMedicineReminder(medicineReminder));
    }

    /**
     * 删除吃药提醒
     */
    @PreAuthorize("@ss.hasPermi('system:reminder:remove')")
    @Log(title = "吃药提醒", businessType = BusinessType.DELETE)
	@DeleteMapping("/{reminderIds}")
    public AjaxResult remove(@PathVariable Long[] reminderIds)
    {
        return toAjax(medicineReminderService.deleteMedicineReminderByReminderIds(reminderIds));
    }


}
